/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package banco;

import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author GEMADA
 */
public class Conexao {

    private static String dbURL = "jdbc:derby://localhost:1527/notaPaulista;create=true;user=sa;password=sa";
    private static String tableName = "GRAVACAO";
    private static Connection conn = null;
    private static Statement stmt = null;

    public static void createConnection() {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
            conn = DriverManager.getConnection(dbURL);
        } catch (Exception except) {
            except.printStackTrace();
        }
    }

    public static void insertTable(String arquivo, String enviado) {
        try {
            stmt = conn.createStatement();
            stmt.execute("insert into " + tableName + " values ('" + arquivo + "','" + enviado + "')");
            stmt.close();
        } catch (SQLException sqlExcept) {
            sqlExcept.printStackTrace();
        }
    }

    public static void selectTable(String arquivo) {
        try {
            stmt = conn.createStatement();
            
            String consulta = "select * from " + tableName + " where arquivo = '" + arquivo + "'";
            
            ResultSet results = stmt.executeQuery(consulta);
            Array teste = results.getArray(1);
            ResultSetMetaData rsmd = results.getMetaData();
            int numberCols = rsmd.getColumnCount();
           
            
//            for (int i = 1; i <= numberCols; i++) {
//                //print Column Names
//                System.out.print(rsmd.getColumnLabel(i) + "\t\t");
//            }
//
//            String teste = results.getString(2);
//            System.out.println("\n-------------------------------------------------");
//
//            while (results.next()) {
//                
//                int id = results.getInt(0);
//                String restName = results.getString(2);
//                String cityName = results.getString(3);
//                System.out.println(id + "\t\t" + restName + "\t\t" + cityName);
//            }
            results.close();
            stmt.close();
        } catch (SQLException sqlExcept) {
            sqlExcept.printStackTrace();
        }
    }

    public static void shutdown() {
        try {
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                DriverManager.getConnection(dbURL + ";shutdown=true");
                conn.close();
            }
        } catch (SQLException sqlExcept) {
        }

    }
}
